<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="图文标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入图文标题"
          clearable
        />
      </el-form-item>
      <el-form-item label="图文内容" prop="infocontent">
        <el-input
          v-model="queryParams.infocontent"
          placeholder="请输入图文内容"
          clearable
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete2"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="封面图片" align="center" prop="photoimage" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.photoimage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="图文内容" align="center" prop="infocontent" show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-html="scope.row.infocontent" style="max-height: 100px;"></div>
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" prop="videourl">
        <template slot-scope="scope">
          <video width="200" controls>
            <source :src="baseUrl + scope.row.videourl" type="video/mp4">
            Your browser does not support the video tag.
          </video>
        </template>
      </el-table-column>
      
      <!-- <el-table-column label="浏览量" align="center" prop="parentUsername" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
          <el-button
          size="mini"
          type="text"
          icon="el-icon-user-solid"
          @click="handleSolid(scope.row)"
          v-hasPermi="['system:role:remove']"
          v-if="scope.row.topFlag == 2"
        >置顶</el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-user"
          @click="handleSolid(scope.row)"
          v-hasPermi="['system:role:remove']"
          v-else
        >取消置顶</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="handleSolid(scope.row)"
            v-hasPermi="['system:role:remove']"
          >置顶</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleStop(scope.row)"
            v-hasPermi="['system:role:remove']"
          >禁用</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
  />

    <!-- 添加或修改用户和角色关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图文标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入图文标题" clearable />
        </el-form-item>
        <!-- <el-form-item label="浏览量" prop="parentUsername">
          <el-input v-model="form.parentUsername" placeholder="请输入浏览量" clearable />
        </el-form-item> -->
        <el-form-item label="封面图片" prop="photoimage">
          <image-upload v-model="form.photoimage" :limit="1"/>
        </el-form-item>
        <el-form-item label="视频上传" prop="videourl">  
          <file-upload v-model="form.videourl" />
      </el-form-item> 
        <el-form-item label="图文内容" prop="infocontent">
          <editor 
            v-model="form.infocontent" 
            :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FileUpload from "@/components/FileUpload";
import { addPlatinfos, listPlatinfos, updatePlatinfos, delPlatinfos } from "@/api/server/platinfos.js"
export default {
  data() {
    return {
      infoSection: 3, // 当前版块
      baseUrl: "https://www.metamirror.cn:8085",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoSection: 3,
        title: "",
        photoimage: "",
        videourl: "",
        infocontent: ""
      },
      showSearch: true,
      single: false,
      multiple: false,
      loading: false,
      currentPage4: 1,
      roleList: [
     
    ],
      total: 0,
      title: "",
      open: false,
      form: {},
      rules: {
        title: [
          { required: true, message: "请输入图文标题", trigger: "blur" }
        ],
        infocontent: [
          { required: true, message: "请输入图文内容", trigger: "blur" }
        ],
        photoimage: [
          { required: true, message: "请选择封面图片", trigger: "blur" }
        ]
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleDelete2(){
      this.$confirm('此操作将永久删除该资讯, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPlatinfos(this.ids.join(',')).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        }).catch((error) => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    getFileName(fileName) {
      this.form.photoimage = fileName;
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {},
    handleExport() {
      this.download('/system/platinfos/export', {
        ...this.queryParams
      }, `禅意动态_${new Date().getTime()}.xlsx`)
    },
    getList() {
      listPlatinfos(this.queryParams).then((response) => {
        this.roleList = response.rows;
        this.total = response.total;
      })
    },
    handleSelectionChange() {},
    // 取消按钮
  cancel() {
    this.open = false;
    this.getList();
    this.reset();
  },
  // 表单重置
  reset() {
    this.form = {
      title: undefined,
      infocontent: undefined,
      photoimage: undefined,
      videourl: undefined
    };
    this.resetForm("form");
  },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.advPlatinfosId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资讯";
    },
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改资讯";
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该资讯, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPlatinfos(row.advPlatinfosId).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.title === '添加资讯') {
            this.form.infoSection = this.infoSection
            console.log(JSON.stringify(this.form))
            addPlatinfos(this.form).then((response) => {
              this.$message({
                type: 'success',
                message: '提交成功!'
              });
              this.open = false;
              this.getList();
            })
          }
          if(this.title === '修改资讯') {
            updatePlatinfos(this.form).then((response) => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.open = false;
              this.getList();
            })
          }
        } else {
          this.$message({
            type: 'error',
            message: '表单验证失败!'
          });
        }
      })
    },
    handleSolid(row) {
      this.$confirm('此操作将置顶该资讯, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (row.topFlag == 2 ){
            row.topFlag = 1; // 设置为置顶
            updatePlatinfos(row).then((response) => {
            this.$message({
              type: 'success',
              message: '置顶成功'
            });
          })
          } else {
            row.topFlag = 2;
            updatePlatinfos(row).then((response) => {
            this.$message({
              type: 'success',
              message: '取消置顶成功'
            });
          })
          }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消置顶'
        });
      });
    },
    handleStop(row) {
      this.$confirm('此操作将永久禁用该资讯, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '禁用成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消禁用'
        });
      });
    },
    
  }
}
</script>

<style>

</style>

